package com.tianjishuju.cimiss.station.station.util;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by felix on 2017/4/28.
 */
public class DateFormatUtil {
    private static Logger logger= LoggerFactory.getLogger(DateFormatUtil.class);
    private final static SimpleDateFormat formatter_SSS = new SimpleDateFormat("yyyyMMddHHmmss:SSS");
    private final static SimpleDateFormat formatter_ss = new SimpleDateFormat("yyyyMMddHHmmss");
    private final static SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
    private final static SimpleDateFormat formatter_second = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private final static SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
    /**
     *
     * @return
     */
    /***
     * 格式化数据
     * @param date yyyyMMddHHmmss
    Date date = new Date(System.currentTimeMillis());
    String dateFormat = DateFormatUtil.getDateFormatess(date);
     * @return
     */
    public static String getDateFormatess(Date date){
        synchronized (formatter_ss){
            return formatter_ss.format(date);
        }
    }
    public static Date getDateFormatess(String date){
        synchronized (formatter_ss){
            try {
                return formatter_ss.parse(date);
            } catch (ParseException e) {
                logger.error("日期格式化错误:"+ ExceptionsUtil.getStackTrace(e));
                return null;
            }
        }
    }
    public static Date getDateFormate(String dateStr){
        Date date=null;
        synchronized(formatter_SSS){
            try{
                date=formatter_SSS.parse(dateStr);
            }catch (Exception e){
                try{
                    date=formatter_ss.parse(dateStr);
                }catch (Exception e1){
                    try {
                        date=formatter.parse(dateStr);
                    }catch (Exception e2){
                        logger.error("日期格式化错误:"+ ExceptionsUtil.getStackTrace(e));
                    }
                }
            }
        }
        return date;
    }

    /***
     * 通过 pattern 格式化日期
     * @param dateStr
     * @param pattern
     * @return
     * 再循环中勿用
     */
    public static Date getDateFormate(String dateStr,String pattern){
        SimpleDateFormat fm=null;
        Date date=null;
        if(pattern!=null){
            fm = new SimpleDateFormat(pattern);
            try {
                date=fm.parse(dateStr);
            } catch (ParseException e) {
                logger.error("日期格式化错误:"+ ExceptionsUtil.getStackTrace(e));
            }
        }else{
            date=getDateFormate(dateStr);
        }
        return date;
    }

    /***
     * 格式化数据
     * @param date
     * @return
     */
    public static String getDateFormateSecond(Date date){
        synchronized (formatter_second){
            return formatter_second.format(date);
        }
    }
    public static Date getDateFormateSecond(String date){
        synchronized (formatter_second){
            try {
                return formatter_second.parse(date);
            } catch (ParseException e) {
                logger.error("日期格式化错误:"+ ExceptionsUtil.getStackTrace(e));
                return null;
            }
        }
    }


    /***
     * 格式化数据yyyy-MM-dd
     * @param date
     * @return
     */
    public static String getDateFormate1(Date date){
        synchronized (formatter1){
            return formatter1.format(date);
        }
    }

    /***
     * 格式化数据yyyy
     * @param date
     * @return
     */
    public static String getYear(Date date){
        String year = new SimpleDateFormat("yyyy").format(date);
        return year;
    }
    /***
     * 格式化数据yyyy-MM
     * @param date
     * @return
     */
    public static String getYearMon(Date date){
        String yearMon = new SimpleDateFormat("yyyy-MM").format(date);
        return yearMon;
    }
    /***
     * 前一天
     * @param date
     * @return
     */
    public static String getBerforeDay(Date date){
        Calendar c = Calendar.getInstance();
        Date d = new Date();
        c.setTime(d);
        int day=c.get(Calendar.DATE);
        c.set(Calendar.DATE,day-1);
        String dayBeforeDay=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
        return dayBeforeDay;
    }

    /***
     * 前一月
     * @param date
     * @return
     */
    public static String getBerforeMon(Date date){
        Calendar c = Calendar.getInstance();
        Date d = new Date();
        c.setTime(d);
        c.add(Calendar.MONTH, -1);

        String dayBeforeMon=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
        return  dayBeforeMon;
    }
}